package tcp_ip;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class SocketClient {
	public static void main(String[] args) throws IOException {
		SocketClient client = new SocketClient();
		BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
		String s;
		while((s = r.readLine()) != null && s.length() > 0) {
			client.send("172.20.230.27", 4444, s.getBytes());
		}
	}
	
	public void send(String server, int serPort, byte[] data) {
		try {
			Socket socket = new Socket(server, serPort);
			
			InputStream in = socket.getInputStream();
			OutputStream out = socket.getOutputStream();
			
			out.write(data);
//			BufferedReader r = new BufferedReader(new InputStreamReader(in));
//			String s;
//			while ((s = r.readLine()) != null) {
//				System.out.println("client recieved: " + s);
//			}
//			int totalBytesRcvd = 0;
//			int byteRcvd;
//			while(totalBytesRcvd < data.length) {
//				if((byteRcvd = in.read(data, totalBytesRcvd, data.length - totalBytesRcvd)) == -1) {
//					throw new SocketException("Connection closed prematurely");
//				}
//				totalBytesRcvd += byteRcvd;
//			}
//			System.out.println("Recieved: " + new String(data));
			socket.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
